/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package com.face.baidu_face.utils;

import com.baidu.ocr.sdk.utils.Parser;
import com.face.baidu_face.exception.FaceException;
import com.face.baidu_face.model.VerifyToken;

import org.json.JSONException;
import org.json.JSONObject;

public class VerifyTokenParser implements Parser<VerifyToken> {
    @Override
    public VerifyToken parse(String json) {
        try {
            VerifyToken verifyToken = new VerifyToken();
            JSONObject jsonObject = new JSONObject(json);

            if (jsonObject != null) {
                JSONObject result = jsonObject.optJSONObject("result");
                if (result != null) {
                    verifyToken.setVerifyToken(result.optString("verify_token"));
                    return verifyToken;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            FaceException error = new FaceException(FaceException.ErrorCode.JSON_PARSE_ERROR, "Json parse error", e);
            try {
                throw error;
            } catch (FaceException ex) {
                throw new RuntimeException(ex);
            }
        }
        return null;
    }
}
